const db = uniCloud.database();
const dbCmd = db.command;
module.exports = {
	_before: function () { // 通用预处理器

	},
	add({
		user_id,
		name,
		tel,
		province,
		city,
		area,
		areaCode,
		detail,
		is_default
	}){
		return db.collection('address').add({
			user_id,
			name,
			tel,
			province,
			city,
			area,
			areaCode,
			detail,
			is_default,
			address:province+city+area+detail
		})
	},
	list(user_id){
		return db.collection('address').where({
			user_id
		}).get()
	},
	async setDefault(user_id,_id){
		//1.首先把所有的该用户地址修改成非默认状态
		let up = await db.collection('address').where({
			user_id:dbCmd.eq(user_id)
		}).update({
			is_default: false
		})  
		//2.把传进去的指定_id的地址设置为默认状态
		// console.error(_id)
		let def = await db.collection('address').doc(_id).update({
			id_default: true
		})
		return def 
	},
	get(_id){
		return db.collection('address').doc(_id).get()
	}
}
